<template>
  <div class="screen" @click="isDelMark = false">
    <div class="rt">
      <span>汇智湖软件园实景监测管理系统</span>
    </div>
    <div class="mainContainer" id="video">
      <video id="videoElement" autoplay>Your browser is too old which doesn't support HTML5 video.</video>
    </div>
    <canvas
      id="canvas"
      @click.right.prevent="canvasClkr($event)"
      @click="canvasClk($event)"
      @dblclick="canvasDClk($event)"
    ></canvas>
    <div :class="'menuMask '+menuCls" @mouseenter="show" @mouseleave="hide">
      <div class="menu">
        <div class="title">设备列表</div>
        <ul>
          <li
            v-for="menu in menuList"
            :key="menu.id"
            :class="menu == currentMenu ? 'active' : ''"
            @click="clickMenu(menu)"
          >
            <a>{{ menu.deviceName }}</a>
          </li>
        </ul>
      </div>
    </div>
    <div
      class="dialog"
      v-show="dialogVisible"
      :style="'top:'+currentMark.top+'px;left:'+currentMark.left+'px'"
      :class="dialogAni"
    >
      <div class="title">信息</div>
      <span @click="dialogVisible=false"></span>
      <h-form :form-data="currentMark" @add="addMark" @cancel="dialogVisible=false"></h-form>
    </div>
    <div
      class="delMark"
      v-show="isDelMark"
      @click="delMark"
      :style="isDelMark?'top:'+currentMark.y+'px;left:'+currentMark.x+'px;':''"
    >删除</div>
    <div class="camerasBox" v-show="camerasBoxVisible">
      <div class="search" style="display:none;">
        <input type="text" placeholder="输入关键字搜索" />
        <span></span>
      </div>
      <div :class="'cameras '+camerasBoxAni">
        <span @click="camerasBoxHide"></span>
        <ul>
          <li>
            <img src="imgs/d1.png" width="108" height="68" />
            <div class="buildInfo">
              <div class="name">{{ currentBuild.name }}</div>
              <div class="address">地址：{{ currentBuild.address }}</div>
              <div class="desc">简介：{{ currentBuild.desc }}</div>
            </div>
            <!-- <div class="info">
              <div class="top">
                <span class="number">陕AF3849</span>
                <span class="time">12:12:12</span>
              </div>
              <div class="rote">
                <span>南京路入口02</span>
              </div>
            </div>-->
          </li>
        </ul>
      </div>
    </div>
    <div
      :class="'dialog '+miniVideoAni"
      v-for="(camera) in cameraList"
      :key="camera.imageId"
      :style="'top:'+camera.top+'px;left:'+camera.left+'px;transform-origin:center center'"
    >
      <div
        v-if="!camera.isWarn"
        class="title"
        style="font-size:12px;padding:3px 0 0 18px;"
      >{{ camera.name }}</div>
      <div v-else class="title" style="font-size:12px;padding:3px 0 0 18px;">告警信息</div>
      <span @click="closeCamera(camera)"></span>
      <div v-if="!camera.isWarn" class="miniVideo">
        <video
          :id="'miniVideo'+camera.imageId"
          autoplay
        >Your browser is too old which doesn't support HTML5 video.</video>
      </div>
      <div v-else class="miniVideo" style="color:#fff;font-size:14px;">12点30分，刷卡异常</div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState, mapGetters } from "vuex";
import Player from "@/externalJs/player.js";
import Mask from "@/externalJs/mask.js";
import flvjs from "@/externalJs/flv.js";
import HForm from "@/components/form.vue";
export default {
  name: "screen",
  components: {
    HForm
  },
  data() {
    return {
      player: null,
      mask: null,
      menuCls: "",
      timer: null,
      dialogVisible: false,
      dialogTop: 0,
      dialogLeft: 0,
      dialogAni: "",
      deviceInfo: {},
      imgEls: [],
      camerasBoxVisible: false,
      camerasBoxAni: "",
      cameraList: [],
      cameraOptions: [],
      miniVideoAni: "dialogShow",
      currentMark: {},
      isDelMark: false,
      currentBuild: {},
      timer1: null,
      minPlayerList: []
    };
  },
  computed: {
    ...mapState({
      currentMenu: state => state.menu.currentMenu,
      currentDevice: state => state.device.currentDevice
    }),
    ...mapGetters({
      menuList: "getMenuList"
    })
  },
  mounted() {
    this.$store.commit("setIsScreen", true);
    this.initMask();
    this.loadMenu();
    let videoBox = document.querySelector("#video");
  },
  updated() {},
  methods: {
    initMask() {
      this.mask = new Mask("#canvas", {
        fillStyle: "rgba(220,38,38,0.2)",
        strokeStyle: "#0000ff",
        inRectCursor: "move",
        bRectsStrokeStyle: "#ff00ff",
        bSideLength: 8
      });
    },
    initPlay(playUrl) {
      this.player = new Player({
        el: "#videoElement",
        url: playUrl,
        flvObj: flvjs
      });
      this.player.init();
      // this.player.play();
    },
    async loadMenu() {
      let that = this;
      let a = 1;
      await axios
        .get("/vpm/device/devicelist", {
          headers: {
            "Content-Type": "application/json"
          }
        })
        .then(resp => {
          if (resp && resp.data) {
            let menuList = resp.data;
            that.$store.commit("setMenuList", menuList);
            that.$store.commit("setCurrentMenu", menuList[0]);
          }
        });

      this.loadDevice(this.currentMenu.deviceCode);

      this.initPlay(this.currentMenu.playUrl);
    },
    loadDevice(deviceCode) {
      let that = this;
      axios
        .get("/vpm/mark/getByDc?deviceCode=" + deviceCode, {
          headers: {
            "Content-Type": "application/json"
          }
        })
        .then(resp => {
          if (resp && resp.data) {
            that.mask.initMark(resp.data);
          }
        });
    },
    clickMenu(menu) {
      this.$store.commit("setCurrentMenu", menu);
      this.loadDevice(menu.deviceCode);
      this.initPlay(menu.playUrl);
    },
    canvasClkr(e) {
      let selMark = this.mask.current(e);
      let left = e.clientX;
      let top = e.clientY;
      let maxX = window.innerWidth;
      let maxY = window.innerHeight;
      let aniType1 = false;
      let aniType2 = false;
      if (left + 258 > maxX) {
        left -= 258;
        aniType1 = true;
      }
      if (top + 206 > maxY) {
        top -= 206;
        aniType2 = true;
      }
      if (selMark == null) {
        // this.dialogVisible = true;
        // this.currentMark = {
        //   top,
        //   left,
        //   x: e.clientX,
        //   y: e.clientY
        // };
        // if (aniType1 && aniType2) {
        //   this.dialogAni = "dialogShow3";
        // } else if (aniType1) {
        //   this.dialogAni = "dialogShow2";
        // } else if (aniType2) {
        //   this.dialogAni = "dialogShow1";
        // } else {
        //   this.dialogAni = "dialogShow";
        // }
        // this.$store.commit("setCurrentDevice", {});
      } else {
        this.currentMark = selMark;
        this.currentMark.x = e.clientX;
        this.currentMark.y = e.clientY;
        this.isDelMark = true;
      }
    },
    addMark({ name, type, subCode }) {
      this.dialogVisible = false;
      this.currentMark.name = name;
      this.currentMark.type = type;
      this.currentMark.subCode = subCode;
      let mark = this.mask.createMark(
        this.currentMark.x,
        this.currentMark.y - 45,
        this.currentMark.name,
        this.currentMark.type
      );
      this.currentMark = {};
      axios.post("vpm/mark/add", {
        markx: mark.w,
        marky: mark.h,
        markSrc: mark.isrc,
        deviceCode: this.currentMenu.deviceCode,
        markName: mark.name,
        markType: mark.itype,
        markCode: mark.imageId,
        markSubDeivceCode: subCode
      });
    },
    delMark() {
      let that = this;
      axios
        .delete(
          "/vpm/mark/delMark?deviceCode=" +
            that.currentMenu.deviceCode +
            "&markCode=" +
            that.currentMark.imageId
        )
        .then(_ => {
          that.loadDevice(that.currentMenu.deviceCode);
        });
    },
    canvasClk(e) {
      this.clearTimer(this.timer1);
      this.timer1 = setTimeout(_ => {
        let selMark = this.mask.current(e);
        if (!selMark) return;
        if (selMark.itype == "warehouse") {
          window.open("http://140.207.49.8:3900/");
          return;
        }
        if (selMark.itype !== "camera") {
          this.camerasBoxAni = "";
        }
        this.infoByType(e, selMark);
      }, 300);
    },
    canvasDClk(e) {
      this.clearTimer(this.timer1);
      let selMark = this.mask.current(e);

      // 计算窗口位置
      this.computePosition(e, selMark);

      if (selMark.markSubDeivceCode == "0017") {
        selMark.isWarn = false;
        //告警
        this.cameraList.push(selMark);
        let playUrl = "http://180.166.77.16:5368/yunyan.flv";
        this.playStream(selMark, playUrl);
      } else {
        this.cameraList.push(selMark);
        // 获取标点信息
        this.loadMarkInfo(selMark).then(result => {
          // 播流
          this.playStream(selMark, result.markPlayUrl);
        });
      }
    },
    async infoByType(e, selMark) {
      if (selMark.itype !== "camera") {
        this.camerasBoxVisible = false;
      }
      this.computePosition(e, selMark);

      // 获取标点信息
      let result = await this.loadMarkInfo(selMark);

      if (selMark.itype === "camera") {
        this.cameraList.push(selMark);
        if (selMark.markSubDeivceCode != "0017") {
          // 播流
          this.playStream(selMark, result.markPlayUrl);
        } else {
          selMark.isWarn = true;
        }
      } else {
        this.camerasBoxAni = "camerasBoxShow";
        this.camerasBoxVisible = true;
        this.currentBuild = {
          desc: (result && result.desc) || "未知",
          address: (result && result.infoAddress) || "未知",
          name: (result && result.infoName) || "未知"
        };
      }
    },
    async loadMarkInfo(selMark) {
      let deviceCode = this.currentMenu.deviceCode;
      let resp = await axios.get(
        "/vpm/mark/getMarkDeviceByCode?deviceCode=" +
          deviceCode +
          "&subDeviceCode=" +
          selMark.markSubDeivceCode
      );

      let result;
      if (resp && resp.data && resp.data.code == "0") {
        result = resp.data.data;
      }

      return result;
    },
    playStream(selMark, playUrl = "http://180.166.77.16:18080/live/s2.flv") {
      let that = this;
      setTimeout(function() {
        try {
          let player = new Player({
            el: "#miniVideo" + selMark.imageId,
            url: playUrl,
            flvObj: flvjs
          });
          player.init();
          that.minPlayerList.push(player);
        } catch {}
        //player.play();
      }, 500);
    },
    computePosition(e, selMark) {
      // 计算窗口位置
      let left = e.clientX;
      let top = e.clientY;
      let maxX = window.innerWidth;
      let maxY = window.innerHeight;
      if (left + 342 > maxX) {
        left -= 342;
      }
      if (top + 234 > maxY) {
        top -= 234;
      }

      selMark.left = left;
      selMark.top = top;
    },
    camerasBoxHide() {
      this.camerasBoxAni = "camerasBoxHide";
    },
    closeCamera(camera) {
      let index = this.cameraList.indexOf(camera);
      this.cameraList.splice(index, 1);
      if (camera.markSubDeivceCode !== "0017")
        this.minPlayerList.splice(index, 1)[0].destroy();
    },
    show() {
      if (this.timer != null) {
        clearTimeout(this.timer);
        this.timer = null;
      }
      this.menuCls = "menuShow";
    },
    hide() {
      this.timer = setTimeout(_ => {
        this.menuCls = "menuHide";
      }, 500);
    },
    clearTimer(timer, type = "timeout") {
      if (timer) {
        type == "timeout" ? clearTimeout(timer) : clearInterval(timer);
        timer = null;
      }
    }
  }
};
</script>

<style lang="less">
@translaX: -215px;
// @translaX: 0px;
.screen {
  height: 100%;
  background-color: #333;

  .rt {
    position: absolute;
    z-index: 9;
    right: 0;
    padding: 20px;
    font-size: 20px;
    /* text-shadow: 3px 3px 2px #666; */
    font-weight: 600;
    font-family: cursive;
  }

  .delMark {
    position: absolute;
    font-size: 12px;
    color: #f56c6c;
    cursor: pointer;
    transform: scale(0.8);
    border: 1px solid #fbc4c4;
    padding: 3px 5px;
    border-radius: 2px;
    background: #fef0f0;
  }

  .miniVideo {
    width: 300px;
    height: 154px;
    margin-top: 20px;
    margin-right: 10px;
  }

  .camerasBox {
    position: absolute;
    height: 100%;
    width: 0;
    top: 0;
    right: 0;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;

    .search {
      width: 100%;
      background: url(/imgs/search.png);
      height: 32px;
      background-size: 100% 100%;

      input {
        background-color: transparent;
        color: #fff;
        font-size: 12px;
        margin: 11px 0 0 23px;
        width: 68%;
        border: 0;
        outline: none;
      }

      span {
        cursor: pointer;
        padding: 0 10px;
        margin: 0 0 0 7px;
      }
    }

    .cameras {
      background: url(/imgs/cameras.png);
      background-size: 100% 100%;
      height: 480px;
      width: 180px;
      position: absolute;
      right: 0;

      & > span {
        padding: 8px;
        position: absolute;
        right: 25px;
        margin-top: 19px;
        cursor: pointer;
      }

      ul {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        height: 93%;
        padding: 54px 22px 0px 30px;
        box-sizing: border-box;

        li {
          background: url(/imgs/details.png);
          width: 128px;
          // height: 120px;
          height: 100%;
          background-size: 100% 100%;
          color: #fff;
          font-size: 12px;
          padding: 10px;
          box-sizing: border-box;
          position: relative;

          .buildInfo {
            height: calc(100% - 78px);

            .name {
              padding: 5px 0;
            }

            .address,
            .desc {
              color: #ccc;
              padding: 5px 0;
            }
          }

          .info {
            display: flex;
            flex-direction: column;
            height: 38px;
            bottom: 0;
            position: absolute;
            width: 100%;
            align-items: flex-start;

            span {
              transform: scale(0.8);
            }

            div {
              &.rote {
                span {
                  transform: scale(0.8) translateX(-3px);
                }
              }

              span {
                display: inline-block;
                transform: scale(0.8);
              }
            }
          }
        }
      }
    }
  }

  .menuMask {
    width: 232px;
    height: 100%;
    position: relative;
    transform: translateX(@translaX);
    display: flex;
    align-items: center;
  }

  .menu {
    padding: 20px;
    width: 100%;
    height: 450px;
    position: absolute;
    color: #fff;
    box-sizing: border-box;
    left: 2px;
    background: url(/imgs/device.png) no-repeat;
    background-size: 100%;

    .title {
      padding: 16px 0 10px 42px;
      font-size: 16px;
    }

    ul {
      height: calc(100% - 50px);
      overflow: auto;

      li {
        padding: 6px 20px;
        cursor: pointer;
        font-size: 12px;
        display: flex;
        flex-direction: row;
        align-items: center;

        &:hover {
          background-color: rgba(68, 247, 255, 0.3);
        }
        &.active {
          background-color: rgba(68, 247, 255, 0.3);
        }

        &::before {
          content: "";
          width: 6px;
          height: 6px;
          border-radius: 50%;
          background-color: lightgreen;
          display: inline-block;
          margin-right: 10px;
        }

        &:nth-of-type(4)::before,
        &:nth-of-type(5)::before {
          background-color: #f00;
        }
      }

      &::-webkit-scrollbar {
        width: 3px;
        height: 1px;
      }

      &::-webkit-scrollbar-thumb {
        // 滚动条上的滚动滑块
        border-radius: 2px;
        box-shadow: inset 0 0 5px rgb(21, 32, 65);
        background: #95a5a6;
      }

      &::-webkit-scrollbar-track-piece {
        // 滚动条没有滑块的轨道部分
        background: transparent;
      }
    }
  }

  .mainContainer {
    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
    overflow: hidden;
  }

  video {
    width: 100%;
    height: 100%;
    object-fit: fill;
  }

  .controls {
    display: block;
    width: 100%;
    text-align: left;
    margin-left: auto;
    margin-right: auto;
  }
  canvas {
    height: 100%;
    width: 100%;
    position: absolute;
  }
  #effect {
    position: relative;
    width: 200px;
    height: 600px;
    padding: 0.4em;
  }
  #effect h5 {
    margin: 0;
    padding: 0.4em;
    text-align: center;
  }
  .leftButton {
    background: red;
    width: 20px;
    height: 80px;
    text-align: center;
    line-height: 20px;
    display: block;
    color: #fff;
  }
  .toggler {
    width: 10px;
    height: 600px;
  }
  .videocss {
    height: auto;
    width: 100%;
  }
  .ui-dialog .ui-dialog-content {
    padding: 0;
  }

  .dialog {
    position: absolute;
    padding: 15px 16px 26px 16px;
    background: url(/imgs/all.png);
    background-size: 100% 100%;

    .title {
      padding: 0 0 0 15px;
      color: #fff;
    }

    & > span {
      padding: 12px;
      top: 0;
      right: 0;
      position: absolute;
      cursor: pointer;
    }

    .btns {
      width: 100%;
      display: flex;
      justify-content: center;
    }
  }

  .menuShow {
    animation-name: menu-show;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
  }

  .menuHide {
    animation-name: menu-hide;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
  }

  @keyframes menu-show {
    from {
      transform: translateX(@translaX);
    }
    to {
      transform: translateX(-2px);
    }
  }

  @keyframes menu-hide {
    from {
      transform: translateX(-2px);
    }
    to {
      transform: translateX(@translaX);
    }
  }

  .dialogShow {
    animation-name: dialog-show;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
    transform-origin: top left;
  }

  .dialogShow1 {
    animation-name: dialog-show;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
    transform-origin: bottom left;
  }

  .dialogShow2 {
    animation-name: dialog-show;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
    transform-origin: top right;
  }

  .dialogShow3 {
    animation-name: dialog-show;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
    transform-origin: bottom right;
  }

  @keyframes dialog-show {
    from {
      transform: scale(0);
    }
    to {
      transform: scale(1);
    }
  }

  .dialogHide {
    animation-name: dialog-hide;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
    transform-origin: top right;
  }

  @keyframes dialog-hide {
    from {
      transform: scale(1);
    }
    to {
      transform: scale(0);
    }
  }

  .camerasBoxShow {
    animation-name: cameras-box-show;
    animation-duration: 500ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
  }

  @keyframes cameras-box-show {
    from {
      transform: rotateY(0deg);
      opacity: 0;
    }
    to {
      transform: rotateY(360deg);
      opacity: 1;
    }
  }

  .camerasBoxHide {
    animation-name: cameras-box-hide;
    animation-duration: 500ms;
    animation-fill-mode: forwards;
    animation-timing-function: linear;
    transform-origin: top right;
  }

  @keyframes cameras-box-hide {
    from {
      transform: scale(1);
      opacity: 1;
    }
    to {
      transform: scale(0);
      opacity: 0;
    }
  }
}
</style>